#include<stdlib.h>
#include<iostream>
using namespace std;

template<class T>
class Vector
{
public:
        Vector(size_t capacity=0):
            _pData((T*)malloc(sizeof(T)*capacity)),
            _size(0),
            _capacity(capacity)
        {}
        
        ~Vector();
        void PushBack(const T&Data)
        {
            CheckCapacity();
            _pData[_size++]=Data;
        }
        
        void PopBack()
        {
            if(_size>0)--_size;
        }

        int size(){return _size;}

        void print()
        {
            cout<<"[ ";
            for(int i=0;i<_size;i++)
            {
                cout<<_pData[i]<<" ";
            }
            cout<<"]"<<endl;
        }
private:
        T *_pData;
        size_t _size;
        size_t _capacity;
        
        void CheckCapacity()
        {
            if(_capacity==_size)
            {
                _capacity=_capacity==0?10:_capacity*2;
                _pData=(T*)realloc(_pData,sizeof(T)*_capacity);
            }   
        }
};
template<class T>
Vector<T>::~Vector()
{
    if(_pData)
        free(_pData);
    _size=_capacity==0;
    _pData=NULL;
}
int main()
{
    Vector<int> nums(10);
    nums.PushBack(100);
    nums.PushBack(200);
    nums.PushBack(300);
    nums.print();
    nums.PopBack();
    nums.PopBack();
    nums.print();
    nums.PushBack(400);
    nums.PushBack(500);
    nums.PopBack();
    nums.PopBack();
    nums.PushBack(600);
    nums.print();
    cout<<"numsSize = "<<nums.size()<<endl;
    Vector<double> numsDouble(5);
    numsDouble.PushBack(1.5);
    numsDouble.PushBack(18923.5);
    numsDouble.PushBack(1.5123);
    numsDouble.PopBack();
    numsDouble.PushBack(1.51);
    numsDouble.PushBack(1.1235);
    numsDouble.PushBack(1.51);
    numsDouble.PushBack(1.5312);
    numsDouble.print();
    cout<<"numsDoubleSize = "<<numsDouble.size()<<endl;
    return 0;
}
